import java.util.ArrayList;
import java.util.List;

public class LetterCombinations {
    // leetcode 电话号码的组合
    // https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/?envType=study-plan-v2&envId=top-100-liked
    List<String> ret;
    StringBuilder path;
    String[] hash = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        ret = new ArrayList<>();
        path = new StringBuilder();
        if (digits == null || digits.length() == 0) return ret;
        dfs(digits, 0);
        return ret;
    }

    private void dfs(String digits, int index) {
        if (path.length() == digits.length()) {
            ret.add(path.toString());
            return;
        }
        for (char c : hash[digits.charAt(index) - '0'].toCharArray()) {
            path.append(c);
            dfs(digits, index + 1);
            path.deleteCharAt(path.length() - 1);
        }
    }
}
